package com.example.handsets8app.Utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import java.util.Locale;

public class VoiceUtils {

    public static VoiceUtils voiceUtils;
    //语音的引入
    private static TextToSpeech mSpeech = null;
    private static float m_voice_speed = 1.0f;   //合成语音的语速
    private static float m_voice_pitch = 1.0f;   //合成语音的语调

    public static VoiceUtils getInstance(){
        if (voiceUtils == null) {
            voiceUtils = new VoiceUtils();

        }
        return voiceUtils;
    }

    public static void init(Context context){
        //获取TTS引擎
            mSpeech = new TextToSpeech(context, new TTSListener());

    }

    //播放合成语音
    public void PlayAudioFile(String str) {
        //设置中文
        int supported = mSpeech.setLanguage(Locale.CHINESE);
        mSpeech.setSpeechRate(m_voice_speed);
        mSpeech.setPitch(m_voice_pitch);

        if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
            //语言设置失败
            Log.i("zyf", "中文设置失败");
        } else {
            Log.i("zyf", "中文设置成功");
        }
        //mSpeech.speak(str, TextToSpeech.QUEUE_FLUSH, null);
        mSpeech.speak(str, TextToSpeech.QUEUE_FLUSH, null);
    }

    //语音引擎
    private static class TTSListener implements TextToSpeech.OnInitListener {
        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if (status == TextToSpeech.SUCCESS) {

                Log.i("zyf", "onInit: TTS引擎初始化成功");
            } else {
                Log.i("zyf", "onInit: TTS引擎初始化失败");
            }
        }
    }

    public void closeVoice(){
//        if (mSpeech != null) {
//            mSpeech = null;
//        }
    }

}
